package com.xiaomi.eco.service.impl;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xiaomi.eco.dal.entity.EcoMethodDimensionEntity;
import com.xiaomi.eco.dal.entity.TbEcoApplicationCompanyEntity;
import com.xiaomi.eco.dal.res.EcoMethodRes;
import com.xiaomi.eco.mapper.EcoMethodDimensionEntityMapper;
import com.xiaomi.eco.mapper.TbEcoApplicationCompanyEntityMapper;
import com.xiaomi.eco.service.EcoMethodDimensionEntityService;
import com.xiaomi.eco.utils.EcoServiceQualityAlarm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLEncoder;
import java.util.*;

@Service
@Slf4j
public class EcoMethodDimensionEntityServiceImpl extends
        ServiceImpl<EcoMethodDimensionEntityMapper, EcoMethodDimensionEntity> implements EcoMethodDimensionEntityService {


    private static final String serverEnvVariable = "~\".*";
    private static final String serverZoneVariable = "~\".*|";

    private static final String serverZoneVariablePrefix = "~\"";

    private static final String horizontalBar = "-";

    private static final String underLine = "_";

    private static final String specialCharacters = "\\s*|\r|\n|\t";


    private static final String msPrefix = "http://prometheus-hera.mioffice.cn/api/v1/query?query=histogram_quantile";

    private static final String httpMethod = "httpMethod";

    private static final String methodName = "methodName";


    private static final String errorNumPrefix = "http://prometheus-hera.mioffice.cn/api/v1/query?query=sum";

    private static final String requestPrefix = "http://prometheus-hera.mioffice.cn/api/v1/query?query=";

    private static final String grpc99UrlTemp = "(0.99,sum(sum_over_time(online_jaeger_grpcServerTimeCost_bucket{application=\"%s\",serverEnv=%s\",serverZone=%s\"}[1d])) by (le,serviceName,methodName))";

    private static final String grpc95UrlTemp = "(0.95,sum(sum_over_time(online_jaeger_grpcServerTimeCost_bucket{application=\"%s\",serverEnv=%s\",serverZone=%s\"}[1d])) by (le,serviceName,methodName))";

    private static final String grpcerrorUrlTemp = "(sum_over_time(online_jaeger_grpcServerError_total{application=\"%s\",serverEnv=%s\",serverZone=%s\"}[1d])) by (methodName) or 0*absent(sum(sum_over_time(online_jaeger_grpcServerError_total{application=\"%s\",serverEnv=%s\",serverZone=%s\"}[1d])) by (methodName))";

    private static final String grpcmethoAvailabilityUrlTemp = "clamp_min((1 - ((sum(sum_over_time(online_jaeger_grpcServerError_total{application=\"%s\",serverEnv=%s\",serverZone=%s\"}[1d])) by (methodName)) / (sum(sum_over_time(online_jaeger_grpcServer_total{application=\"%s\",serverEnv=%s\",serverZone=%s\"}[1d])) by (methodName)))), 0) or 0*(sum(sum_over_time(online_jaeger_grpcServer_total{application=\"%s\",serverEnv=%s\",serverZone=%s\"}[1d])) by (methodName))+1";

    private static final String grpctotalUrlTemp = "sum(sum_over_time(online_jaeger_grpcServer_total{application=\"%s\",serverEnv=%s\",serverZone=%s\"}[1d])) by (methodName,serviceName)";

    private static final String httpp95UrlTemp = "(0.95,sum(sum_over_time(online_jaeger_aopMethodTimeCount_bucket{application=\"%s\",serverEnv=%s\",serverZone=%s\"}[1d])) by (le,methodName, httpMethod))";

    private static final String httperrorUrlTemp = "(sum_over_time(online_jaeger_httpError_total{application=\"%s\",serverEnv=%s\",serverZone=%s\"}[1d])) by (methodName, httpMethod) or 0*absent(sum(sum_over_time(online_jaeger_httpError_total{application=\"%s\",serverEnv=%s\",serverZone=%s\"}[1d])) by (methodName, httpMethod))";

    private static final String httpmethoAvailabilityUrlTemp = "clamp_min((1 - ((sum(sum_over_time(online_jaeger_httpError_total{application=\"%s\",serverEnv=%s\",serverZone=%s\"}[1d])) by (methodName, httpMethod)) / (sum(sum_over_time(online_jaeger_aopTotalMethodCount_total{application=\"%s\",serverEnv=%s\",serverZone=%s\"}[1d])) by (methodName, httpMethod)))), 0) or 0*(sum(sum_over_time(online_jaeger_aopTotalMethodCount_total{application=\"%s\",serverEnv=%s\",serverZone=%s\"}[1d])) by (methodName, httpMethod))+1";

    private static final String httptotalUrlTemp = "sum(sum_over_time(online_jaeger_aopTotalMethodCount_total{application=\"%s\",serverEnv=%s\",serverZone=%s\"}[1d])) by (methodName, httpMethod)";

    private static final String httpp99UrlTemp = "(0.99,sum(sum_over_time(online_jaeger_aopMethodTimeCount_bucket{application=\"%s\",serverEnv=%s\",serverZone=%s\"}[1d])) by (le,methodName, httpMethod))";


    @Autowired
    private RestTemplate restTemplate;


    @Autowired
    private TbEcoApplicationCompanyEntityMapper tbEcoApplicationCompanyEntityMapper;


    @Autowired
    EcoMethodDimensionEntityMapper ecoMethodDimensionEntityMapper;


    @Override
    public void insertEcoService() {
        StringBuffer p99Num = new StringBuffer();
        StringBuffer methoAvailability = new StringBuffer();
        StringBuffer P95Num = new StringBuffer();
        StringBuffer totalNum = new StringBuffer();
        ObjectMapper objectMapper = new ObjectMapper();
        StringBuffer errorNum = new StringBuffer();
        long lcurrentTime = System.currentTimeMillis();

        List<TbEcoApplicationCompanyEntity> list = tbEcoApplicationCompanyEntityMapper.selectList(null);
        LambdaUpdateWrapper<EcoMethodDimensionEntity> lambdaUpdateWrapper = new LambdaUpdateWrapper<EcoMethodDimensionEntity>();

        ecoMethodDimensionEntityMapper.deleteByToday();
        for (TbEcoApplicationCompanyEntity tbeac : list) {
            String applicationIdName = tbeac.getApplicationId().replaceAll(specialCharacters, "") + underLine + tbeac.getApplicationName().replaceAll(specialCharacters, "").replaceAll(horizontalBar, underLine);
            List<EcoMethodDimensionEntity> p95List = new ArrayList<>();
            P95Num.append(msPrefix);
            String p95Url = "";
            //先判断是不是grcp
            //在判断机房区域
            if (tbeac.getRequestProtocol().equals("grpc")) {
                if (tbeac.getServerZone().isEmpty() || tbeac.getServerZone().equals("")) {
                    p95Url = String.format(grpc95UrlTemp, applicationIdName, serverEnvVariable, serverZoneVariable);
                } else {
                    p95Url = String.format(grpc95UrlTemp, applicationIdName, serverEnvVariable, serverZoneVariablePrefix + tbeac.getServerZone());
                }
            } else if (tbeac.getRequestProtocol().equals("http")) {
                if (tbeac.getServerZone().isEmpty() || tbeac.getServerZone().equals("")) {
                    p95Url = String.format(httpp95UrlTemp, applicationIdName, serverEnvVariable, serverZoneVariable);
                } else {
                    p95Url = String.format(httpp95UrlTemp, applicationIdName, serverEnvVariable, serverZoneVariablePrefix + tbeac.getServerZone());
                }
            }

            String resultP95Url = EncodeUrl(P95Num, p95Url);
            EcoMethodRes p95MethodRes = ParseUrl(objectMapper, resultP95Url);
            EcoMethodRes.Data.Result[] p95Count = p95MethodRes.getData().getResult();

            //分别遍历每个服务下的接口
            if (p95Count != null) {
                for (EcoMethodRes.Data.Result result : p95Count) {
                    EcoMethodDimensionEntity ecoMethodDimensionEntity = new EcoMethodDimensionEntity();
                    ecoMethodDimensionEntity.setApplicationId(tbeac.getApplicationId());
                    double time = result.getValue()[1];
                    if (time < EcoServiceQualityAlarm.p95timeNum) {
                        String p95Result = String.valueOf(time * 1000);
                        ecoMethodDimensionEntity.setTakesTimeP95(p95Result);
                    } else {
                        String p95Result = String.valueOf(time);
                        ecoMethodDimensionEntity.setTakesTimeP95(p95Result);
                    }

                    Map<String, Object> metric = result.getMetric(); // 假设 getMetric() 方法存在
                    for (Map.Entry<String, Object> entry : metric.entrySet()) {
                        if (entry.getKey().equals(httpMethod)) {
                            ecoMethodDimensionEntity.setHttpMethodType((String) entry.getValue());
                        }
                    if (entry.getKey().equals(methodName)) {
                            ecoMethodDimensionEntity.setHttpMethodName((String) entry.getValue());
                        }
                    }
                    ecoMethodDimensionEntity.setSaveTime(lcurrentTime);
                    ecoMethodDimensionEntity.setServerZone(tbeac.getServerZone());
                    ecoMethodDimensionEntity.setIsSelfStudy(tbeac.getIsSelfStudy());
                    p95List.add(ecoMethodDimensionEntity);
                }
                saveBatch(p95List);
            }

            //能不能做到一次性调用5个请求
            errorNum.append(errorNumPrefix);
            String errorUrl = "";
            if (tbeac.getRequestProtocol().equals("grpc")) {
                if (tbeac.getServerZone().isEmpty() || tbeac.getServerZone().equals("")) {
                    errorUrl = String.format(grpcerrorUrlTemp, applicationIdName, serverEnvVariable, serverZoneVariable, applicationIdName, serverEnvVariable, serverZoneVariable);
                } else {
                    errorUrl = String.format(grpcerrorUrlTemp, applicationIdName, serverEnvVariable, serverZoneVariablePrefix + tbeac.getServerZone(), applicationIdName, serverEnvVariable, serverZoneVariablePrefix + tbeac.getServerZone());
                }
            } else if (tbeac.getRequestProtocol().equals("http")) {
                if (tbeac.getServerZone().isEmpty() || tbeac.getServerZone().equals("")) {
                    errorUrl = String.format(httperrorUrlTemp, applicationIdName, serverEnvVariable, serverZoneVariable, applicationIdName, serverEnvVariable, serverZoneVariable);
                } else {
                    errorUrl = String.format(httperrorUrlTemp, applicationIdName, serverEnvVariable, serverZoneVariablePrefix + tbeac.getServerZone(), applicationIdName, serverEnvVariable, serverZoneVariablePrefix + tbeac.getServerZone());
                }
            }
            String resultErrorUrl = EncodeUrl(errorNum, errorUrl);
            EcoMethodRes errorMethodRes = ParseUrl(objectMapper, resultErrorUrl);//请求一个接口地址,里面的小循环，循环一次创建一个该对象
            EcoMethodRes.Data.Result[] errorCount = errorMethodRes.getData().getResult();
            //分别遍历每个服务下的接口
            if (errorCount != null) {
                for (EcoMethodRes.Data.Result result : errorCount) {
                    //一个接口一个对象 比如有9000条数据 就创建9000多个对象
                    Map<String, Object> metric = result.getMetric(); // 假设 getMetric() 方法存在
                    String httpMethodType = "";
                    String httpMethodName = "";
                   // System.out.println("metric "+metric.toString());
                    for (Map.Entry<String, Object> entry : metric.entrySet()) {
                        if (entry.getKey().equals(httpMethod)) {
                            httpMethodType = (String) entry.getValue();
                    }
                    if (entry.getKey().equals(methodName)) {
                            httpMethodName = (String) entry.getValue();
                        }
                    }
                    if(httpMethodType.isEmpty()){
                        lambdaUpdateWrapper.clear();
                        lambdaUpdateWrapper.set(EcoMethodDimensionEntity::getNumberOfErrors, String.valueOf(result.getValue()[1]))
                                .eq(EcoMethodDimensionEntity::getApplicationId, tbeac.getApplicationId())
                                .eq(EcoMethodDimensionEntity::getHttpMethodName, httpMethodName)
                                .eq(EcoMethodDimensionEntity::getSaveTime, lcurrentTime)
                                .eq(EcoMethodDimensionEntity::getServerZone, tbeac.getServerZone())
                                .eq(EcoMethodDimensionEntity::getIsSelfStudy, tbeac.getIsSelfStudy());
                        update(lambdaUpdateWrapper);
                    }else {
                        lambdaUpdateWrapper.clear();
                        lambdaUpdateWrapper.set(EcoMethodDimensionEntity::getNumberOfErrors, String.valueOf(result.getValue()[1]))
                                .eq(EcoMethodDimensionEntity::getApplicationId, tbeac.getApplicationId())
                                .eq(EcoMethodDimensionEntity::getHttpMethodType, httpMethodType)
                                .eq(EcoMethodDimensionEntity::getHttpMethodName, httpMethodName)
                                .eq(EcoMethodDimensionEntity::getSaveTime, lcurrentTime)
                                .eq(EcoMethodDimensionEntity::getServerZone, tbeac.getServerZone())
                                .eq(EcoMethodDimensionEntity::getIsSelfStudy, tbeac.getIsSelfStudy());
                        update(lambdaUpdateWrapper);
                    }
                }
            }


            methoAvailability.append(requestPrefix);
            String methoAvailabilityUrl = "";
            if (tbeac.getRequestProtocol().equals("grpc")) {
                if (tbeac.getServerZone().isEmpty() || tbeac.getServerZone().equals("")) {
                    methoAvailabilityUrl = String.format(grpcmethoAvailabilityUrlTemp, applicationIdName, serverEnvVariable, serverZoneVariable, applicationIdName, serverEnvVariable, serverZoneVariable, applicationIdName, serverEnvVariable, serverZoneVariable);
                } else {
                    methoAvailabilityUrl = String.format(grpcmethoAvailabilityUrlTemp, applicationIdName, serverEnvVariable, serverZoneVariablePrefix + tbeac.getServerZone(), applicationIdName, serverEnvVariable, serverZoneVariablePrefix + tbeac.getServerZone(), applicationIdName, serverEnvVariable, serverZoneVariablePrefix + tbeac.getServerZone());
                }
            } else if (tbeac.getRequestProtocol().equals("http")) {
                if (tbeac.getServerZone().isEmpty() || tbeac.getServerZone().equals("")) {
                    methoAvailabilityUrl = String.format(httpmethoAvailabilityUrlTemp, applicationIdName, serverEnvVariable, serverZoneVariable, applicationIdName, serverEnvVariable, serverZoneVariable, applicationIdName, serverEnvVariable, serverZoneVariable);
                } else {
                    methoAvailabilityUrl = String.format(httpmethoAvailabilityUrlTemp, applicationIdName, serverEnvVariable, serverZoneVariablePrefix + tbeac.getServerZone(), applicationIdName, serverEnvVariable, serverZoneVariablePrefix + tbeac.getServerZone(), applicationIdName, serverEnvVariable, serverZoneVariablePrefix + tbeac.getServerZone());
                }
            }
            String resultAvailabilityUrl = EncodeUrl(methoAvailability, methoAvailabilityUrl);
            EcoMethodRes availabilityecoMethodRes = ParseUrl(objectMapper, resultAvailabilityUrl);
            EcoMethodRes.Data.Result[] availabilityCount = availabilityecoMethodRes.getData().getResult();

            //分别遍历每个服务下的接口
            if (availabilityCount != null) {
                for (EcoMethodRes.Data.Result result : availabilityCount) {
                    //一个接口一个对象 比如有9000条数据 就创建9000多个对象
                    String s = String.valueOf(result.getValue()[1] * 100) + "%";
                    if (s.split("\\.")[1].length() > 6) {
                        s = s.split("\\.")[0] + "." + s.split("\\.")[1].substring(0, 5) + "%";
                    }
                    Map<String, Object> metric = result.getMetric(); // 假设 getMetric() 方法存在
                    String httpMethodType = "";
                    String httpMethodName = "";
                    for (Map.Entry<String, Object> entry : metric.entrySet()) {
                        if (entry.getKey().equals(httpMethod)) {
                            httpMethodType = (String) entry.getValue();
                        }
                    if (entry.getKey().equals(methodName)) {
                            httpMethodName = (String) entry.getValue();
                        }
                    }
                    if(httpMethodType.isEmpty()){
                        lambdaUpdateWrapper.clear();
                        lambdaUpdateWrapper.set(EcoMethodDimensionEntity::getMethodAvailability, s)
                                .eq(EcoMethodDimensionEntity::getApplicationId, tbeac.getApplicationId())
                                .eq(EcoMethodDimensionEntity::getHttpMethodName, httpMethodName)
                                .eq(EcoMethodDimensionEntity::getSaveTime, lcurrentTime)
                                .eq(EcoMethodDimensionEntity::getServerZone, tbeac.getServerZone())
                                .eq(EcoMethodDimensionEntity::getIsSelfStudy, tbeac.getIsSelfStudy());
                        update(lambdaUpdateWrapper);
                    }else {
                        lambdaUpdateWrapper.clear();
                        lambdaUpdateWrapper.set(EcoMethodDimensionEntity::getMethodAvailability, s)
                                .eq(EcoMethodDimensionEntity::getApplicationId, tbeac.getApplicationId())
                                .eq(EcoMethodDimensionEntity::getHttpMethodType, httpMethodType)
                                .eq(EcoMethodDimensionEntity::getHttpMethodName, httpMethodName)
                                .eq(EcoMethodDimensionEntity::getSaveTime, lcurrentTime)
                                .eq(EcoMethodDimensionEntity::getServerZone, tbeac.getServerZone())
                                .eq(EcoMethodDimensionEntity::getIsSelfStudy, tbeac.getIsSelfStudy());
                        update(lambdaUpdateWrapper);
                    }

                }
            }


            totalNum.append(requestPrefix);
            String totalUrl = "";
            if (tbeac.getRequestProtocol().equals("grpc")) {
                if (tbeac.getServerZone().isEmpty() || tbeac.getServerZone().equals("")) {
                    totalUrl = String.format(grpctotalUrlTemp, applicationIdName, serverEnvVariable, serverZoneVariable);
                } else {
                    totalUrl = String.format(grpctotalUrlTemp, applicationIdName, serverEnvVariable, serverZoneVariablePrefix + tbeac.getServerZone());
                }
            } else if (tbeac.getRequestProtocol().equals("http")) {
                if (tbeac.getServerZone().isEmpty() || tbeac.getServerZone().equals("")) {
                    totalUrl = String.format(httptotalUrlTemp, applicationIdName, serverEnvVariable, serverZoneVariable);
                } else {
                    totalUrl = String.format(httptotalUrlTemp, applicationIdName, serverEnvVariable, serverZoneVariablePrefix + tbeac.getServerZone());
                }
            }
            String resultTotalUrl = EncodeUrl(totalNum, totalUrl);
            EcoMethodRes totalecoMethodRes = ParseUrl(objectMapper, resultTotalUrl);
            EcoMethodRes.Data.Result[] totalCount = totalecoMethodRes.getData().getResult();

            //分别遍历每个服务下的接口
            if (totalCount != null) {
                for (EcoMethodRes.Data.Result result : totalCount) {
                    //一个接口一个对象 比如有9000条数据 就创建9000多个对象
                    String httpMethodType = "";
                    String httpMethodName = "";
                    Map<String, Object> metric = result.getMetric();
                    for (Map.Entry<String, Object> entry : metric.entrySet()) {
                        if (entry.getKey().equals(httpMethod)) {
                            httpMethodType = (String) entry.getValue();
                        }
                    if (entry.getKey().equals(methodName)) {
                            httpMethodName = (String) entry.getValue();
                        }
                    }
                    if(httpMethodType.isEmpty()){
                        lambdaUpdateWrapper.clear();
                        lambdaUpdateWrapper.set(EcoMethodDimensionEntity::getTotalCalls, (long) result.getValue()[1])
                                .eq(EcoMethodDimensionEntity::getApplicationId, tbeac.getApplicationId())
                                .eq(EcoMethodDimensionEntity::getHttpMethodName, httpMethodName)
                                .eq(EcoMethodDimensionEntity::getSaveTime, lcurrentTime)
                                .eq(EcoMethodDimensionEntity::getServerZone, tbeac.getServerZone())
                                .eq(EcoMethodDimensionEntity::getIsSelfStudy, tbeac.getIsSelfStudy());
                        update(lambdaUpdateWrapper);
                    }else {
                        lambdaUpdateWrapper.clear();
                        lambdaUpdateWrapper.set(EcoMethodDimensionEntity::getTotalCalls, (long) result.getValue()[1])
                                .eq(EcoMethodDimensionEntity::getApplicationId, tbeac.getApplicationId())
                                .eq(EcoMethodDimensionEntity::getHttpMethodType, httpMethodType)
                                .eq(EcoMethodDimensionEntity::getHttpMethodName, httpMethodName)
                                .eq(EcoMethodDimensionEntity::getSaveTime, lcurrentTime)
                                .eq(EcoMethodDimensionEntity::getServerZone, tbeac.getServerZone())
                                .eq(EcoMethodDimensionEntity::getIsSelfStudy, tbeac.getIsSelfStudy());
                        update(lambdaUpdateWrapper);
                    }

                }
            }

            p99Num.append(msPrefix);
            String p99Url = "";
            if (tbeac.getRequestProtocol().equals("grpc")) {
                if (tbeac.getServerZone().isEmpty() || tbeac.getServerZone().equals("")) {
                    p99Url = String.format(grpc99UrlTemp, applicationIdName, serverEnvVariable, serverZoneVariable);
                } else {
                    p99Url = String.format(grpc99UrlTemp, applicationIdName, serverEnvVariable, serverZoneVariablePrefix + tbeac.getServerZone());
                }
            } else if (tbeac.getRequestProtocol().equals("http")) {
                if (tbeac.getServerZone().isEmpty() || tbeac.getServerZone().equals("")) {
                    p99Url = String.format(httpp99UrlTemp, applicationIdName, serverEnvVariable, serverZoneVariable);
                } else {
                    p99Url = String.format(httpp99UrlTemp, applicationIdName, serverEnvVariable, serverZoneVariablePrefix + tbeac.getServerZone());
                }
            }
            String resultP99Url = EncodeUrl(p99Num, p99Url);
            EcoMethodRes p99ecoMethodRes = ParseUrl(objectMapper, resultP99Url);//循环一个服务 就返回一堆数据
            EcoMethodRes.Data.Result[] p99Count = p99ecoMethodRes.getData().getResult();


            //分别遍历每个服务下的接口
            if (p99Count != null) {
                for (EcoMethodRes.Data.Result result : p99Count) {
                    //一个接口一个对象 比如有9000条数据 就创建9000多个对象
                    double time = result.getValue()[1];
                    String p99Result = "";
                    if (time < EcoServiceQualityAlarm.p99TimeNum) {
                        p99Result = String.valueOf(time * 1000);
                    } else {
                        p99Result = String.valueOf(time);
                    }

                    Map<String, Object> metric = result.getMetric(); // 假设 getMetric() 方法存在
                    String httpMethodType = "";
                    String httpMethodName = "";
                    System.out.println("grcp: "+metric);
                    for (Map.Entry<String, Object> entry : metric.entrySet()) {
                        if (entry.getKey().equals(httpMethod)) {
                            httpMethodType = (String) entry.getValue();
                        }
                    if (entry.getKey().equals(methodName)) {
                            httpMethodName = (String) entry.getValue();
                        }
                    }
                    if(httpMethodType.isEmpty()){
                        lambdaUpdateWrapper.clear();
                        lambdaUpdateWrapper.set(EcoMethodDimensionEntity::getTakesTimeP99, p99Result)
                                .eq(EcoMethodDimensionEntity::getApplicationId, tbeac.getApplicationId())
                                .eq(EcoMethodDimensionEntity::getHttpMethodName, httpMethodName)
                                .eq(EcoMethodDimensionEntity::getSaveTime, lcurrentTime)
                                .eq(EcoMethodDimensionEntity::getServerZone, tbeac.getServerZone())
                                .eq(EcoMethodDimensionEntity::getIsSelfStudy, tbeac.getIsSelfStudy());
                        update(lambdaUpdateWrapper);
                    }else {
                        lambdaUpdateWrapper.clear();
                        lambdaUpdateWrapper.set(EcoMethodDimensionEntity::getTakesTimeP99, p99Result)
                                .eq(EcoMethodDimensionEntity::getApplicationId, tbeac.getApplicationId())
                                .eq(EcoMethodDimensionEntity::getHttpMethodType, httpMethodType)
                                .eq(EcoMethodDimensionEntity::getHttpMethodName, httpMethodName)
                                .eq(EcoMethodDimensionEntity::getSaveTime, lcurrentTime)
                                .eq(EcoMethodDimensionEntity::getServerZone, tbeac.getServerZone())
                                .eq(EcoMethodDimensionEntity::getIsSelfStudy, tbeac.getIsSelfStudy());
                        update(lambdaUpdateWrapper);
                    }

                }
            }
        }
    }



    /*
     * 调用接口
     * @param ObjectMapper objectMapper, String result_url
     * return EcoMethodRes
     * */
    public EcoMethodRes ParseUrl(ObjectMapper objectMapper, String resultUrl) {
        try {
            EcoMethodRes ecoMethodRes = objectMapper.readValue(resultUrl, EcoMethodRes.class);
            return ecoMethodRes;
        } catch (JsonProcessingException e) {
            log.error("读取url错误: {}");
            return null;
        }
    }

    /*
     * 解析url
     * @param StringBuffer pre_url, String encryption_url
     * return String
     * */
    public String EncodeUrl(StringBuffer preUrl, String encryptionUrl) {
        try {
            String encode_url = URLEncoder.encode(encryptionUrl, "UTF-8");
            String currentTime = Long.toString(System.currentTimeMillis());
            String currenttimePrecent = currentTime.substring(0, currentTime.length() - 3) + "." + currentTime.substring(currentTime.length() - 3);
            preUrl.append(encode_url).append("&time=").append(currenttimePrecent);
            System.out.println("sumsum: " + preUrl);
            String jsonString = restTemplate.getForObject(URI.create(preUrl.toString()), String.class);
            preUrl.setLength(0);
            return jsonString;
        } catch (UnsupportedEncodingException e) {
            log.error("解析url错误: {}");
            return null;
        }
    }

}
